package com.fudan.es.aggs.handler;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.Sum;
import org.springframework.stereotype.Component;

import com.fudan.es.Aggs;
import com.fudan.es.aggs.handler.base.AggsHandler;
import com.fudan.es.entity.DimensionPack;
import com.fudan.es.entity.FieldMeta;
import com.fudan.es.entity.MetricPack;
@Component
public class SumHandler extends AggsHandler{

	@Override
	public AggregationBuilder handle(DimensionPack dimension) {
		return null;
	}

	@Override
	public AggregationBuilder build(MetricPack metric) {
		FieldMeta field = metric.getField();
		return Aggs.sum(field);
	}

	@Override
	public Map<String, Object> handle(Map<String, Object> map, Aggregations aggregations, MetricPack metricPack) {
		if(aggregations == null) return map;
		Integer id = metricPack.getField().getId();
		Sum sum = aggregations.get(getAggregationName(metricPack));
		Map<String,Object> atom = new HashMap<String, Object>();
		atom.put(SUM_TAG, sum.getValue());
		map.put(id.toString(),atom);
		return atom;
	}

}
